function buildMenu(menu, menuList, menuIds) {
	let nextLayer = []
	for (let i = menu.length - 1; i > -1; i--) {
		const currentMenu = menu[i]
		const subMenu = menuList.filter(item => {
			if (item.superior_id === currentMenu.id) {
				menuIds.push(item.id)
				return true
			}
		})
		nextLayer = nextLayer.concat(subMenu)
		currentMenu.children = subMenu
	}
	if (nextLayer.length) {
		buildMenu(nextLayer, menuList, menuIds)
	}
}

//递归子分类
function onqueryload(data,main_id,association_id,add_id) {
	for (var i = 0; i < data.length; i++) {
		let item = data[i]
		const depth = getParents(data, item[main_id], 0, main_id, association_id)
		item.name = (depth ? '　'.repeat(depth) + '' : '') + item.name
		item.depth = depth;
		item.depth_to = false;
	}
	const menuTree = getSortMenu(data,main_id,association_id)
	const sortMenus = []
	patTree(menuTree, sortMenus)
	data.length = 0;
	data.push(...sortMenus)
	var list = data 
	// this.list = data  
	// console.log("this.list: " + JSON.stringify(this.list));
	//新增刷新
	if (add_id > 0) {
		for (var i = 0; i < list.length; i++) {
			if (list[i][main_id] == add_id) {
				var parent_id = list[i][association_id]
				menu_selected(parent_id,main_id,association_id);
			}
		}
	}else{
		return list
	}
}
function menu_selected(parent_id,main_id,association_id) {
	for (var i = 0; i < this.list.length; i++) {
		if (parent_id == this.list[i][main_id]) {
			var parent_ids = this.list[i][association_id]
			this.list[i]['depth_to'] = true;
			if (parent_ids > 0) {
				this.menu_selected(parent_ids,main_id,association_id);
			}
		}
		if (parent_id == this.list[i][this.association_id]) {
			this.list[i]['expand'] = true;
		}

	}
}

// 获取父的个数
function getParents(menus, id, depth = 0, main_id, association_id) {
	menus.forEach(menu => {
		if (menu[main_id] === id && menu[association_id]) {
			depth = depth + 1 + getParents(menus, menu[association_id], depth, main_id, association_id)

		}
	})

	return depth
}

function patTree(tree, sortMenus) {
	tree.forEach(item => {
		sortMenus.push(item)
		if (item.children.length) {
			patTree(item.children, sortMenus)
		}
	})
	return sortMenus
}

function getParentIds(menuItem, menuList) {
	const parentArr = []
	let currentItem = menuItem
	while (currentItem && currentItem.category_id) {
		parentArr.push(currentItem.category_id)
		currentItem = menuList.find(item => item.id === currentItem.category_id)
	}
	return parentArr
}

function getSortMenu(menuList, main_id, association_id) {
	// 标记叶子节点
	menuList.map(item => {
		if (!menuList.some(subMenuItem => subMenuItem[association_id] === item[main_id])) {
			item.isLeafNode = true
		}
	})
	return buildMenus(menuList)
}

function buildMenus(menuList, trim = true) {
	// 保证父子级顺序
	menuList = menuList.sort(function(a, b) {
		const parentIdsA = getParentIds(a, menuList)
		const parentIdsB = getParentIds(b, menuList)
		if (parentIdsA.includes(b.id)) {
			return 1
		}
		return parentIdsA.length - parentIdsB.length || a.sort - b.sort
	})
	// 删除无subMenu且非子节点的菜单项
	if (trim) {
		for (let i = menuList.length - 1; i > -1; i--) {
			const currentMenu = menuList[i]
			const subMenu = menuList.filter(subMenuItem => subMenuItem.superior_id === currentMenu.id)
			if (!currentMenu.isLeafNode && !subMenu.length) {
				menuList.splice(i, 1)
			}
		}
	}
	const menuIds = []
	const menu = menuList.filter(item => {
		if (!item.superior_id) {
			menuIds.push(item.id)
			return true
		}
	})
	buildMenu(menu, menuList, menuIds)
	// 包含所有无效菜单
	if (!trim && menuIds.length !== menuList.length) {
		menu.push(...menuList.filter(item => !menuIds.includes(item.id)))
	}
	return menu
}

export {
	buildMenu,
	buildMenus,
	getParents,
	patTree,
	getSortMenu,
	onqueryload
}